package com.springboot.mybatis.common.redis;

import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;



@Component
public class RedisUtil {
    
    public Logger log = LogManager.getLogger(RedisUtil.class);
    
    
    @Resource
    StringRedisTemplate stringRedisTemplate;
    
    public Boolean set(String key, String value, Long expireTime, TimeUnit unit){
        try {
            stringRedisTemplate.opsForValue().set(key, value, expireTime, unit);
            return true;
        } catch (Exception e) {
            log.info("设置redis报错：" + e.toString());
        }
        return false;
    }
    
    public String get(String key){
        try {
            return stringRedisTemplate.opsForValue().get(key);
        } catch (Exception e) {
            log.info("获取redis报错：" + e.toString());
        }
        return null;
    }
    
    public String getAndSet(String key, String value){
        try {
            return stringRedisTemplate.opsForValue().getAndSet(key, value);
        } catch (Exception e) {
            log.info("获取redis报错：" + e.toString());
        }
        return null;
    }
    
    public Long getExpire(String key){
        try {
            return stringRedisTemplate.getExpire(key);
        } catch (Exception e) {
            log.info("获取过期时间redis报错：" + e.toString());
        }
        return null;
    }
    
    public Long getExpire(String key, TimeUnit unit){
        try {
            return stringRedisTemplate.getExpire(key, unit);
        } catch (Exception e) {
            log.info("获取过期时间redis报错：" + e.toString());
        }
        return null;
    }
}
